# -*- coding: utf-8 -*-
__author__ = 'Paul'
from five import grok

from plone.app.layout.viewlets.interfaces    import IAboveContent,IPortalFooter
from plone.app.layout.globals.interfaces    import IViewView
from Products.bpTheme.interfaces            import IBpTheme
from zope.interface                         import Interface
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from plone.app.layout.globals.interfaces    import IViewView
from Products.ATContentTypes.interface      import IATFolder
from zope.site.hooks                        import getSite
from zope.component                         import getMultiAdapter
from Products.CMFCore.utils                 import getToolByName
from Products.bpContents.galeria            import IGaleria

# Import default Plone viewlet classes
from plone.app.layout.viewlets              import common as base


class CustomFooter(base.ViewletBase):
    """Footer de la biblioteca"""
    def update(self):
        pass

    def getGalerias(self):
        """
        Devuelve un diccionario creado con los H2 y H3 del cuerpo de texto
        que se elige
        """
        from Products.bpContents.getImgTira import GetTiraImgs
        inicio=getSite()
        catalog = getToolByName(inicio, 'portal_catalog')
        listado= []

        topChrs=110
        folder_path = '/'.join(inicio["home"]["galerias"].getPhysicalPath())
        for brain in catalog({'object_provides':IGaleria.__identifier__},path={'query': folder_path, 'depth': 1}):
            descI=0
            idn=0
            tmpPal=brain.Description.split(" ")
            decripcion=""
            while descI<topChrs:
                decripcion+=tmpPal[idn]+" "
                descI=len(decripcion)
                idn=idn+1
                if len(tmpPal)==idn:
                    break;


            if brain.getObject().tipoGaleria=="Multimedia":
                tmpBrain=dict()
                tmpBrain["titulo"]=brain.Title
                tmpBrain["descri"]= decripcion
                tmpBrain["url"]=brain.getURL()
                tmpBrain["img"]=GetTiraImgs(brain.getObject()).getImagenDestacada()
                listado.append(tmpBrain)

        if len(listado)>0:
            return listado[:2]
        else:
            return None


    def getDepartamentos(self):
        """Listado de Departametos"""
        mainF = getSite()
        try:
            if hasattr(mainF["home"]["conocenos"], 'areas'):
                deptF = mainF["home"]["conocenos"]["areas"]
            elif hasattr(mainF["home"]["conocenos"], 'departamentos'):
                deptF = mainF["home"]["conocenos"]["departamentos"]
        except:
            return None

        catalog=getToolByName(mainF,"portal_catalog")
        listado=[]
        result=catalog({"path":"/".join(deptF.getPhysicalPath()),
                        'sort_on': 'sortable_title',
                        'portal_type':('Products.bpContents.departamento',)})
        for brain in result:
            tmpBrain=dict()
            tmpBrain["titulo"]=brain.Title
            tmpBrain["url"]=brain.getURL()
            listado.append(tmpBrain)
        if len(listado)>0:
            return listado
        else:
            return  None
